package com.xdja.myapplication.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import com.xdja.myapplication.R

/**
 * @program: My Application
 * @description:
 * @author: flyou
 * @email: fzl@xdja.com
 * @create: 2019-09-19 10:04
 */
class RecyclerViewAdapter(private val mDatas: List<String>) :
    RecyclerView.Adapter<RecyclerViewAdapter.VH>() {
    private  var listener: OnItemClickListener?=null
    override fun onBindViewHolder(holder: VH, position: Int) {
        holder.title.text = mDatas[position]
        holder.itemView.setOnClickListener {
            listener?.onItemClick(position)
        }
    }

    override fun getItemCount(): Int {
        return mDatas.size
    }


    class VH(v: View) : RecyclerView.ViewHolder(v) {
        val title: TextView = v.findViewById(R.id.title)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_item, parent, false)
        return VH(v)
    }

    interface OnItemClickListener{
        fun onItemClick(position:Int)
    }

    fun setOnItemClickListener(listener:OnItemClickListener){
        this.listener=listener
    }
}
